home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / program / firstbas.zip / BALL.BAS next >
BASIC Source File  |  1996-08-01  |  4KB  |  114 lines

  1. '┌───────────────────────────────────────────────────────────────────────────┐
  2. '│                                  BALL.BAS                                 │
  3. '│                                                                           │
  4. '│   This  program is a simple demonstration of the graphics capabilities    │
  5. '│ of FirstBASIC.  It displays a "bouncing ball" that uses random numbers    │
  6. '│ to figure out which directions to bounce off to.                          │
  7. '│                                                                           │
  8. '│  In order to run this program do the following:                           │
  9. '│    1. Load FirstBasic by typing FB at the DOS prompt.                     │
  10. '│    2. Load the file BALL.BAS from the Load option of the File             │
  11. '│       pulldown menu.                                                      │
  12. '│    3. Compile and run the program by pressing F9.                         │
  13. '└───────────────────────────────────────────────────────────────────────────┘
  14.  
  15.  
  16. '  logic:
  17. '    draw the ball
  18. '    use GET to store pixels into an array
  19. '    set CurrentPosition = OldPosition = StartPoint
  20. '    DO
  21. '      Erase (PUT with XOR) the object at the OldPosition
  22. '      CurrentPosition = CurrentPosition + Increment
  23. '      Display (PUT) the object at the CurrentPosition
  24. '      DELAY a small amount of time
  25. '      OldPosition = CurrentPosition
  26. '    LOOP UNTIL any key is hit
  27. '    end of program
  28. '
  29. DEFINT A-Z
  30. RANDOMIZE TIMER
  31. '
  32. '  dimension the save buffer for the pixels
  33. '
  34. DIM GraphicsBuffer(1000)
  35.  
  36. SCREEN 1
  37. '
  38. '  set screen min and max based on screen number
  39. '
  40. Max.X = 319 : Min.X = 0
  41. Max.Y = 199 : Min.Y = 0
  42. '
  43. '  set size of ball
  44. '
  45. SizeOfBall = 15
  46. '
  47. '  set up the starting center position for the ball
  48. '
  49. Start.X = 15
  50. Start.Y = 15
  51. '
  52. '  build the ball on the screen
  53. '
  54. CIRCLE (Start.X,Start.Y),SizeOfBall,2
  55. PAINT (Start.X,Start.Y),1,2
  56. '
  57. '  store the pixels in a graphics save buffer
  58. '
  59. GET (Start.X-SizeOfBall,Start.Y-SizeOfBall)-(Start.X+SizeOfBall,Start.Y+SizeOfBall),GraphicsBuffer
  60. '
  61. '  initialize the position of the ball
  62. '
  63. CurrentPosition.X = OldPosition.X = Start.X
  64. CurrentPosition.Y = OldPosition.Y = Start.Y
  65. '
  66. '  set current X direction to Right, Y direction to Down
  67. '
  68. Direction.X = 1
  69. Direction.Y = 1
  70.  
  71. DO
  72.   '
  73.   '  erase previous ball by doing a PUT at the old position
  74.   '
  75.   PUT (OldPosition.X,OldPosition.Y),GraphicsBuffer
  76.   '
  77.   '  calculate new X position,
  78.   '  if at right edge set direction to Left
  79.   '  if at left edge set direction to right
  80.   '  if ball hits an edge, make a sound
  81.   '
  82.   Increment.X = RND*8
  83.   IF CurrentPosition.X+Increment.X+30 > Max.X THEN Direction.X = -1 : sound 200+rnd*250,.5
  84.   IF CurrentPosition.X-Increment.X < Min.Y THEN Direction.X = 1 : sound 200+rnd*300,.5
  85.   CurrentPosition.X = CurrentPosition.X + (Increment.X*Direction.X)
  86.   '
  87.   '  calculate new Y position,
  88.   '  if at bottom edge set direction to the up
  89.   '  if at top edge set direction to the down
  90.   '  if ball hits an edge, make a sound
  91.   '
  92.   Increment.Y = RND*8
  93.   IF CurrentPosition.Y+Increment.Y+30 > Max.Y THEN Direction.Y = -1 : sound 200+rnd*275,.5
  94.   IF CurrentPosition.Y-Increment.Y < Min.Y THEN Direction.Y = 1 : sound 200+rnd*325,.5
  95.   CurrentPosition.Y = CurrentPosition.Y + (Increment.Y*Direction.Y)
  96.   '
  97.   '  display the ball at the new position
  98.   '
  99.   PUT (CurrentPosition.X,CurrentPosition.Y),GraphicsBuffer
  100.   '
  101.   '  wait some time for smoother animation
  102.   '
  103.   DELAY .03
  104.   '
  105.   '  save current position so the ball can be erased before next move
  106.   '
  107.   OldPosition.X = CurrentPosition.X
  108.   OldPosition.Y = CurrentPosition.Y
  109.   '
  110.   '  keep looping until any key is hit
  111.   '
  112. LOOP UNTIL INSTAT
  113. END
  114.